import requests
import re
import os
import shutil
import time
from util.logger import get_looger

logger = get_looger("豆瓣250.log", __file__)
logger.info("启动")

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36'
}


try:
    root = 'static/豆瓣250/'
    if os.path.exists(root):
        shutil.rmtree(root)
    os.makedirs(root)

    for start in range(0, 250, 25):
        logger.debug(f"正在爬取第{(start + 25) // 25}页")

        url = f'https://movie.douban.com/top250?start={start}&filter='

        response = requests.get(url, headers=headers)

        content = response.text

        items = re.findall(r'<div class="pic">.*?href="(.*?)">.*?alt="(.*?)" src="(.*?)">.*?</div>', content, re.S)
        for item in items:
            time.sleep(1)
            link = item[0]
            title = item[1]
            image_url = item[2]
            logger.info(link, title, image_url)
            logger.debug(f"正在爬取第{(start + 25) // 25}页的 电影{title} 的详情")
            detail_response = requests.get(link, headers=headers)
            detail_content = detail_response.text
            summary = re.search(r'<span class="all hidden">(.*?)</span>', detail_content, re.S)
            if summary:
                summary_text = ""
                for text in summary.group(1).strip().split('<br />'):
                    summary_text += text.strip()
            else:
                summary = re.search(r'<span property="v:summary">(.*?)</span>', detail_content, re.S)
                summary_text = ""
                for text in summary.group(1).strip().split('<br />'):
                    summary_text += text.strip()

            logger.debug(f"正在爬取第{(start + 25) // 25}页的 电影{title} 图片")

            image_response = requests.get(image_url, headers=headers)
            with open(f'{root}/{title}.png', 'wb') as f:
                f.write(image_response.content)

except Exception as e:
    logger.error(e)

logger.info("爬取结束")